
package com.ruoyi.web.controller.lingshui.conpermit;


import java.util.List;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.exception.LSBusinessException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.util.ShiroUtils;
import com.ruoyi.supervise.domain.BaseBussinessRecord;
import com.ruoyi.supervise.domain.BaseConsturctionCertInfo;
import com.ruoyi.supervise.domain.ConstructionPermitChangeApply;
import com.ruoyi.supervise.domain.ConstructionPermitRecord;
import com.ruoyi.supervise.domain.CrOperaterecordinfo;
import com.ruoyi.supervise.service.IBaseConsturctionCertInfoService;
import com.ruoyi.supervise.service.IConstructionPermitChangeService;
import com.ruoyi.supervise.service.IConstructionPermitRecordService;
import com.ruoyi.system.domain.SysUser;

import cn.hutool.core.util.StrUtil;

/**
 * @ClassName: ConstructionPermitRecordController
 * @Description: 施工许可审查记录controller
 * @Author wtj
 * @DateTime 2020年4月13日 下午12:01:32
 */
@Controller
@RequestMapping("/conpermit/reviewrecord")
public class ConstructionPermitRecordController extends BaseController{

	private String prefix = "lingshui/conpermit/reviewrecord";
	
	private String changeExaminePrefix = "lingshui/conpermit/changeExamine";
	
	private String journalPrefix = "lingshui/journal/conpermit";
	
	@Autowired
    private IConstructionPermitChangeService constructionPermitChangeService;
	
	@Autowired
    private IConstructionPermitRecordService constructionPermitRecordService;
	
	@Autowired
	private IBaseConsturctionCertInfoService baseConsturctionCertInfoService;
	
	@RequiresPermissions("conpermit:reviewrecord:view")
	@GetMapping()
	public String constructionPermitRecord()
	{
	    return prefix + "/constructionPermitRecord";
	}
	
	/**
     * 查询核查信息列表
     */
    @RequiresPermissions("conpermit:reviewrecord:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(ConstructionPermitRecord cpr)
    {
        startPage();
        // 获取当前的用户信息
        SysUser currentUser = ShiroUtils.getSysUser();
        if(!currentUser.getLoginName().equals("admin") && !currentUser.getLoginName().trim().equals("lingshui")) {
        	cpr.setInChargeDepartmentGuid(currentUser.getDept().getGuid());
        }
        List<ConstructionPermitRecord> cprList = constructionPermitRecordService.selectConstructionPermitRecordTable(cpr);
        for(ConstructionPermitRecord cp:cprList) {
        	String[] subTotalCompany=StrUtil.split(cp.getTotalCompany(), "|");
        	if(subTotalCompany.length == 2) {
        		cp.setTotalCompanyGuid(subTotalCompany[0]);
        		cp.setTotalCompanyName(subTotalCompany[1]);
        	}else {
        		cp.setTotalCompany("");
        		cp.setTotalCompanyGuid("");
        		cp.setTotalCompanyName("");
        	}
        }
        return getDataTable(cprList);
    }
    
    /**
     * @Title: export
     * @Description: 导出审查记录
     * @Author wtj
     * @DateTime 2020-9-25 11:57:12
     * @param cpr
     * @return
     */
    @RequiresPermissions("conpermit:reviewrecord:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(ConstructionPermitRecord cpr)
    {
    	List<ConstructionPermitRecord> cprList = constructionPermitRecordService.selectConstructionPermitRecordTable(cpr);
        for(ConstructionPermitRecord cp:cprList) {
        	String[] subTotalCompany=StrUtil.split(cp.getTotalCompany(), "|");
        	if(subTotalCompany.length == 2) {
        		cp.setTotalCompanyGuid(subTotalCompany[0]);
        		cp.setTotalCompanyName(subTotalCompany[1]);
        	}else {
        		cp.setTotalCompany("");
        		cp.setTotalCompanyGuid("");
        		cp.setTotalCompanyName("");
        	}
        }
        ExcelUtil<ConstructionPermitRecord> util = new ExcelUtil<ConstructionPermitRecord>(ConstructionPermitRecord.class);
        return util.exportExcel(cprList, "审查记录");
    }
    
    /**
     * @Title: certificateTable
     * @Description: 跳转到变更列表
     * @Author wtj
     * @DateTime 2020-9-25 11:57:40
     * @param guid
     * @param mmap
     * @return
     */
    @RequiresPermissions("conpermit:reviewrecord:change")
    @GetMapping("/changeTable/{guid}")
    public String certificateTable(@PathVariable("guid") String guid,ModelMap mmap)
    {
 	   mmap.put("guid", guid);
 	   return prefix + "/changeTable";
    }
    
    /**
     * @Title: changeTableInfo
     * @Description: 变更列表获取数据
     * @Author wtj
     * @DateTime 2020-9-25 11:57:54
     * @param guid
     * @return
     */
    @RequiresPermissions("conpermit:reviewrecord:change")
    @PostMapping("/changeTableInfo/{guid}")
    @ResponseBody
    public TableDataInfo changeTableInfo(@PathVariable("guid") String guid)
    {
        startPage();
        List<BaseBussinessRecord> bbrList = constructionPermitChangeService.selectConPermitChangeExamineListByDataGuid(guid);
        return getDataTable(bbrList);
    }
    
    /**
     * @Title: changeExamineDetail
     * @Description: 变更详情
     * @Author wtj 
     * @DateTime 2020-9-25 11:58:06
     * @param guid
     * @param mmap
     * @return
     */
    @RequiresPermissions("conpermit:reviewrecord:change")
    @GetMapping("/changeExamineDetail/{guid}")
    public String changeExamineDetail(@PathVariable("guid") String guid,ModelMap mmap)
    {
    	ConstructionPermitChangeApply cpca = constructionPermitChangeService.selectConPermitChangeExamineDetailByGuid(guid);
    	mmap.put("compactBeginDate", DateUtils.parseDate(cpca.getCompactBeginDate()));
    	mmap.put("reportDate", DateUtils.parseDate(cpca.getReportDate()));
    	mmap.put("cpca", cpca);
    	mmap.put("isclose", "false");
		return changeExaminePrefix + "/constructionPermitChangeApply";
    }
    
    /**
     * @Title: certificateDetail
     * @Description: 证书详情
     * @Author wtj
     * @DateTime 2020-9-25 11:58:39
     * @param guid
     * @param mmap
     * @return
     */
    @RequiresPermissions("conpermit:reviewrecord:cert")
    @GetMapping("/certificateDetail/{guid}")
    public String certificateDetail(@PathVariable("guid") String guid,ModelMap mmap)
    {
    	BaseConsturctionCertInfo bcci = baseConsturctionCertInfoService.selectConsturctionCertInfoByGuid(guid);
    	bcci.setIssueDate(bcci.getIssueDate().substring(0, 10));
    	mmap.put("bcci", bcci);
    	return prefix + "/certificateDetail";
    }
    
    /**
     * @Title: conpermitJournal
     * @Description: 获取日志信息列表
     * @Author wtj
     * @DateTime 2020-9-25 11:58:54
     * @param guid
     * @param mmap
     * @return
     */
    @RequiresPermissions("conpermit:reviewrecord:log")
    @GetMapping("/conpermitJournal/{guid}")
    public String conpermitJournal(@PathVariable("guid") String guid,ModelMap mmap)
    {
    	List<CrOperaterecordinfo> coLosi = constructionPermitRecordService.selectConstructionPermitJournal(guid);
    	if(StringUtils.isEmpty(coLosi)) {
    		throw new LSBusinessException("数据获取不正确，请确认选择的是否正确");
    	}
    	mmap.put("coLosi", coLosi);
    	return journalPrefix + "/conpermitJournal";
    }
    
}
